package com.citygis.transferdata.tools;

import javax.security.auth.callback.TextOutputCallback;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName: DateUtil 
 * @Description: TODO 时间日期工具
 * @author: liuzg
 * @Date: 2021/7/29 10:08
 * @Version: v1.0
 */
public class DateUtil {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    /**
     * 计算时间差
     *
     * @return
     */
    public static String getTtimeDifference(String current, String last) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = null;
        java.util.Date date = null;
        try {
            now = df.parse(current);
            date = df.parse(last);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long l = now.getTime() - date.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

        String differ = day + "天" + hour + "小时" + min + "分" + s + "秒";
        System.out.println(differ);
        return differ;
    }

    /**
     * 获取当前时间 + 输入的小时
     *
     * @param hourse
     * @return
     */
    public static String getDate(Integer hourse) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        // 24小时制
        cal.add(Calendar.HOUR, hourse);
        return sdf.format(cal.getTime());
    }

}
